﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;
using Newtonsoft.Json;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseCorpQAForCpp : System.Web.UI.Page
    {
        protected string CorpUserGuid
        {
            get
            {
                return Request["corpUserGuid"] == null ? "" : Request["corpUserGuid"];
            }
        }
        protected string AreaCode
        {
            get
            {
                return Request["areaCode"] == null ? "" : Request["areaCode"];
            }
        }
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
                BindData();
                BindFiles();
            }
        }
        private void InitPage()
        {
            hdnCorpUserGuid.Value = CorpUserGuid;
        }

        private void BindData()
        {
            if (!string.IsNullOrEmpty(CorpUserGuid))
            {
                var currentAreaCode = "";
                ////获取企业信息，登陆账户与企业不一致
                string corpUserGuid = CorpUserGuid;
                var corpInfo = GetCompanyinfo(corpUserGuid);
                if (corpInfo == null)
                {
                    return;
                }
                ltrCorpName.Text = corpInfo.CorpName;
                var khCorpInfo = JsonConvert.DeserializeObject<Kh_CorpTable>(enterpriseServiceClient.GetCorpSecurity(corpUserGuid, currentAreaCode));
                ////安全生产许可证
                hdnkhAreaCode.Value = corpInfo.AreaCode;
                hdnkhCorpUserguid.Value = corpInfo.CorpUserGuid.ToString();
                hdnkhCorpName.Value = corpInfo.CorpName;
                if (khCorpInfo != null)
                {
                    txtSecurityCode.Text = khCorpInfo.SafeLicenceNo;
                    txtSecurityTitleGetDate.Value = khCorpInfo.LicenceBeginDate.HasValue ? ((DateTime)khCorpInfo.LicenceBeginDate).ToShortDateString() : "";
                    txtEndDate.Value = khCorpInfo.LicenceEndDate.HasValue ? ((DateTime)khCorpInfo.LicenceEndDate).ToShortDateString() : "";
                }
            }
        }

        private Tb_Corp_CompanyInfo GetCompanyinfo(string corpUserGuid)
        {
            QueryCompanyInfoCondition queryCompanyInfoCondition =
               new QueryCompanyInfoCondition() { CorpUserGuid = new ParameterValue<Guid>() { V = Guid.Parse(corpUserGuid), O = OptEnum.Equal } };
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
            queryCompanyInfoList.Add(queryCompanyInfoCondition);
            var corpInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(queryCompanyInfoList), AreaCode));
            return corpInfo;
        }

        private void BindFiles()
        {
            string objectGuid = CorpUserGuid;
            string sysCode = "EF";
            string funcCode = "CorpAQ";
            if ((!string.IsNullOrEmpty(objectGuid)) && (!string.IsNullOrEmpty(sysCode)) && (!string.IsNullOrEmpty(funcCode)))
            {
                using (FileService.FileWebServiceSoapClient client = new FileService.FileWebServiceSoapClient())
                {
                    FileService.ArrayOfString files = client.GetFileList(objectGuid, sysCode, funcCode);
                    rptImgs.DataSource = files;
                    rptImgs.DataBind();
                }
            }
        }

        public string GetImgUrl(object fileName)
        {
            string sysCode = "EF";
            string funcCode = "CorpAQ";
            return string.Format("../Handlers/GetFileHandler.ashx?fileName={0}&sysCode={1}&funcCode={2}&rand ={3}", fileName, sysCode, funcCode, Guid.NewGuid());
        }

        public string OpenNewPageShowFile(object fileName)
        {
            string sysCode = "EF";
            string funcCode = "CorpAQ";
            return string.Format("UploadFileShowNewpage.aspx?fileName={0}&sysCode={1}&funcCode={2}&rand ={3}", fileName, sysCode, funcCode, Guid.NewGuid()); 
        }
    }
}